package com.example.utils.util;


import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.*;

import java.io.File;
import java.util.Date;
import java.util.Iterator;

/**
 * TODO   删除过期文件的工具类
 *
 * @ClassName DeleteFiles
 * @Author xiaojiang
 * @Date 2021/12/7 下午 16:35
 */
public class DeleteFiles {

        public static void deleFile(){

            String oss = System.getProperty("os.name");
            String pth = "/usr/local/jetty/webapp/ROOT/";
            if (oss.toLowerCase().startsWith("win")) {
                pth = "D:/test/";
                System.out.println("当前系统是：" + oss);
            } else {
                pth = "/usr/local/jetty/webapp/ROOT/";
            }
            String path1 = pth;
            // 该日期之前的文件
            Date pointDate = new Date(System.currentTimeMillis() - 14400000);  // 毫秒
            System.out.println("该日期之前的文件是：" + pointDate);
            // 文件过滤条件
            IOFileFilter timeFileFilter = FileFilterUtils.ageFileFilter(pointDate, true);
            IOFileFilter fileFiles = new AndFileFilter(FileFileFilter.FILE, timeFileFilter);
            File directory = new File(path1);
            Iterator<File> itFile = FileUtils.iterateFiles(directory, fileFiles, TrueFileFilter.INSTANCE);
            // 删除符合条件的文件
            while (itFile.hasNext()) {
                itFile.next().delete();
            }

        }

    public static void main(String[] args) {
        deleFile();
    }

}
